/*
 * Copyright (c) 2008 Anshuman Manur <anshuman.manur@gmail.com>,
 * Lavanya Shastri <lavanya.shastri@gmail.com>
 * 
 * This file is part of Andronova.
 * 
 * Andronova is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Andronova is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with Andronova.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package com.googlecode.andronova.helper;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyHandler extends DefaultHandler {

	private boolean inTitle;
	private boolean inAlbum;
	private boolean inArtist;
	private boolean inLyrics;
	
	private ParsedDataSet myParsedDataSet;

	private static final String LOCAL_NAME_TITLE = "tt";
	private static final String LOCAL_NAME_ARTIST = "ar";
	private static final String LOCAL_NAME_ALBUM = "al";
	private static final String LOCAL_NAME_LYRICS = "tx";
	
	public ParsedDataSet getParsedDataSet() {
		return myParsedDataSet;
	}
	
	@Override
	public void startDocument() throws SAXException {
		myParsedDataSet = new ParsedDataSet();		
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		
		if(inTitle)
			myParsedDataSet.setTitle(new String(ch, start, length));
		else if(inAlbum)
			myParsedDataSet.setAlbum(new String(ch, start, length));
		else if(inArtist)
			myParsedDataSet.setArtist(new String(ch, start, length));
		else if(inLyrics)
			myParsedDataSet.setLyrics(new String(ch, start, length));
	}

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		
		if(localName.equals(LOCAL_NAME_ARTIST))
			inArtist = false;
		else if(localName.equals(LOCAL_NAME_ALBUM))
			inAlbum = false;
		else if(localName.equals(LOCAL_NAME_TITLE))
			inTitle = false;
		else if(localName.equals(LOCAL_NAME_LYRICS))
			inLyrics = false;
	}

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		
		if(localName.equals(LOCAL_NAME_ARTIST))
			inArtist = true;
		else if(localName.equals(LOCAL_NAME_ALBUM))
			inAlbum = true;
		else if(localName.equals(LOCAL_NAME_TITLE))
			inTitle = true;
		else if(localName.equals(LOCAL_NAME_LYRICS))
			inLyrics = true;		
	}	
}
